package com.skeyedu.servlet;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.skeyedu.dao.UserDao;
import com.skeyedu.dao.UserDaoImpl;
import com.skeyedu.pojo.Users;
import com.skeyedu.utils.MD5utils;
import com.skeyedu.utils.SendMail;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Description 描述
 * @Author 羽落陌殇
 * @Date 2021/7/22 16:22
 * @Version 1.0.0
 */
public class UserServlet extends HttpServlet {
    private UserDao userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (StrUtil.isNotBlank(method) && "regist".equals(method)) {
            createUser(req, resp);
        } else if (StrUtil.isNotBlank(method) && "active".equals(method)) {
            activeUser(req,resp);
        } else if (StrUtil.isNotBlank(method) && "login".equals(method)) {
            login(req,resp);
        }
    }

    // 用户登录
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Users users = null;
        if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(password)) {
            try {
                users = userDao.getLoginUser(username, MD5utils.encrypt(password));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ObjectUtil.isNotEmpty(users)) {
            req.setAttribute("msg", "登录成功");
            try {
                req.getRequestDispatcher("info.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        } else {
            req.setAttribute("msg", "登录失败");
            try {
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 激活用户账号
    private void activeUser(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getParameter("uid");
        int status = 1;
        int result = 0;
        if (StrUtil.isNotBlank(uid)) {
            // 激活账号, 简单来说就是修改用户状态
            try {
               result = userDao.updateUserStatus(status, uid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (result > 0) {
            req.setAttribute("msg", "用户激活成功");
            try {
                req.getRequestDispatcher("info.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        } else {
            req.setAttribute("msg", "用户激活失败");
            try {
                req.getRequestDispatcher("info.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void createUser(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        Users users = null;
        if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(email) && StrUtil.isNotBlank(password)) {
            users = new Users(UUID.randomUUID().toString().toUpperCase(), username, email, MD5utils.encrypt(password), 0);
            int result = 0;
            try {
                result = userDao.createUser(users);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (result > 0) {
                req.setAttribute("msg", "注册成功");
                try {
                    SendMail sendMail = new SendMail(users);
                    // 执行线程
                    sendMail.start();
                    req.getRequestDispatcher("info.jsp").forward(req, resp);
                } catch (ServletException | IOException e) {
                    e.printStackTrace();
                }
            } else {
                req.setAttribute("msg", "注册失败");
                try {
                    req.getRequestDispatcher("info.jsp").forward(req, resp);
                } catch (ServletException | IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            req.setAttribute("msg", "用户名、邮箱、密码都不能为空");
            try {
                req.getRequestDispatcher("index.jsp").forward(req, resp);
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test() {
        System.out.println(UUID.randomUUID().toString().toUpperCase());
    }
}
